//
//  NLWelcomeViewController.swift
//  sinaWeibo7110
//
//  Created by 安静清晨 on 2017/1/9.
//  Copyright © 2017年 walk-in-minds. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


private let bottonMargin = 160
private let iconWH = 90
class NLWelcomeViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        
    
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        setupUI()
//        startAnimation()

    }
    
    
    //在视图已经显示的时候再调用动画, 这样的效果是已经出现在下方的头像往上移动. 否则, 头像会从控制器左上角移动,因为旧的frame(由约束推导出的)布局了以后马上布局新的frame
    override func viewDidAppear(_ animated: Bool) {
        
        startAnimation()
    }
    
    
    
    
    
    private func startAnimation(){
    
        //更新约束, 然后layoutifNeeded
        //原理: 更新约束---只是收集约束. 更新约束这一动作不会直接改变控件的frame. 只有当viewdidlayoutsubviews时, 控制器会根据约束过的控件的位置来推算出frame.
    
//        let offset = -(bottonMargin + iconWH)
        userIcon.snp.updateConstraints { (make) in
            make.centerY.equalTo(self.view).offset(-100)
        }
    
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
        }) { (_ ) in
            
            UIView.animate(withDuration: 0.5, animations: { 
                
                self.welcomeLbl.alpha = 1
                
            }, completion: { (_ ) in
                
//                UIApplication.shared.keyWindow?.rootViewController = NLTabBarController()
//                
                NotificationCenter.default.post(name: NSNotification.Name.AppChangeRootVC, object: "NLTabBarController")
                
                
            })
            
        }
    
    }
    
    
    private func setupUI(){
        
        view.addSubview(userIcon)
        view.addSubview(welcomeLbl)
        
        //约束
        userIcon.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view).offset(bottonMargin)
            make.size.equalTo(CGSize(width: iconWH, height: iconWH))
        }
        
        
        welcomeLbl.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(userIcon)
            make.top.equalTo(userIcon.snp.bottom).offset(20)
        }
        
        //圆形头像
        userIcon.layer.cornerRadius = 45
        userIcon.layer.masksToBounds = true
        
        //可以将url封装在viewModel里
        let url = URL(string: NLUserAccountViewModel.shared.account?.avatar_large ?? "")
        userIcon.sd_setImage(with: url)
        
    }

    //用户头像
    
    lazy var userIcon : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    
    lazy var welcomeLbl : UILabel = {
        
        let lbl = UILabel(title: "\(NLUserAccountViewModel.shared.account?.screen_name ?? "") 欢迎回来", BGImageName: "", size: 14, color: UIColor.orange)
        
            lbl.textColor = UIColor.orange
        
//        let lbl = UILabel(title: "\(NLUserAccountViewModel.shared.account?.screen_name)欢迎回来", BGImageName: "", size: 14, color: .white)
//
        
//        let lbl = UILabel()
//        lbl.text = "\(NLUserAccountViewModel.shared.account?.screen_name)欢迎回来"
//        lbl.font = UIFont.systemFont(ofSize: 14)
//        lbl.textColor = UIColor.white
//        
            lbl.alpha = 0
        return lbl
    }()
    
    
}
